﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Demo.Framework
{
    public class SkySphere : IDisposable
    {
        readonly BasicModel _skySphereModel;
        readonly Effect _effect;
        readonly GraphicsDevice _graphics;
        private readonly TextureCube _textureCube;

        public SkySphere(ContentManager content, GraphicsDevice graphicsDevice)
        {
            _skySphereModel = new BasicModel(content.Load<Model>("Models/skysphere_mesh"), graphicsDevice);
            _textureCube = content.Load<TextureCube>("Textures/Skybox");

            _effect = content.Load<Effect>("Effects/skysphere_effect");
            _effect.Parameters["CubeMap"].SetValue(_textureCube);

            _skySphereModel.SetModelEffect(_effect, false);

            this._graphics = graphicsDevice;
        }

        public void Draw()
        {
            // Disable the depth buffer
            _graphics.DepthStencilState = DepthStencilState.None;

            // Move the model with the sphere
            _skySphereModel.SetPosition(GameEngine.Instance.Camera.Position, Matrix.Identity);

            _skySphereModel.Draw();

            _graphics.DepthStencilState = DepthStencilState.Default;
        }

        public void Dispose()
        {
            _skySphereModel.Dispose();
            _effect.Dispose();
            _textureCube.Dispose();
        }
    }
}
